<?php
/*
 * 配送方式
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\Shipping as ShippingModel;
use think\facade\Db;

class Shipping extends AdminBase
{
    public function lists()
    {
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $where = function ($query) use ($param){$query->where('name','LIKE', '%'.trim($param['title']).'%');};
        }else{
            $where = 1;
        }
        $list = ShippingModel::where($where)->paginate();
        return $this->fetch('index',['list'=>$list]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'shipping');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = ShippingModel::create($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('添加了配送方式');
            $this->success('添加成功');
        }
        return $this->fetch('save');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'shipping');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = ShippingModel::update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('修改了配送方式');
            $this->success('修改成功');
        }
        $data = Db::table('shipping')->find(input('id'));
        $rules_arr = explode(',',$data['rules']);
        $region = Db::table('region')->field('id,name title,pid')->where(['status' => 1])->select()->toArray();
        foreach ($region as $k => $v) {
            if(in_array($v['id'],$rules_arr) && in_array($v['pid'],$rules_arr)){
                $counts = Db::table('region')->where(['pid'=>$v['id'],'status' => 1])->count('id');
                if(!$counts) $region[$k]['checked'] = true;
            }
        }
        return $this->fetch('save',['data' => $data, 'region' => list_to_tree($region)]);
    }

    public function status()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            ShippingModel::where('is_default',1)->where('id','<>',$param['id'])->update(['is_default'=>0]);//修改原默认配送方式为普通
            $result = ShippingModel::update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('修改了默认配送方式');
            $this->success('修改成功');
        }
        return $this->fetch('save',['data' => Db::table('shipping')->find(input('id'))]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Db::table('shipping')->where('is_default',$param['id'])->find() && $this->error('默认配送方式，无法删除');
            ShippingModel::destroy($param['id']);
            insert_admin_log('删除了配送方式');
            $this->success('删除成功');
        }
    }




}
